package edu.bw.web.role;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.service.RoleService;
import edu.bw.service.impl.RoleServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/role/deleteRoleByIds")
public class DeleteRoleByIdsServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收前端的参数
        //{"ids":[13,12,11]}
        String reqJSON = request.getReader().readLine();
        List<Integer> list = ((JSONArray)JSONUtil.parseObj(reqJSON).get("ids")).toList(Integer.class);

        //2.调用service层处理数据
        Integer rows = roleService.deleteRoleByIds(list);

        //3.将删除结果写给前端
        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
